﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WP09S03_OfficeTracking
{
    public partial class DiagAddSupplies : WP09S03_OfficeTracking.ParentClass.ParentDiag
    {
        public DiagAddSupplies()
        {
            InitializeComponent();
        }

 

        private void DiagAddSupplies_Load(object sender, EventArgs e)
        {
            this.categoryTableAdapter.FillByConsumable(dSItem.Category, 1);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            pErrorProvider.Clear();
            if (cboCategory.SelectedItem == null)
            {
                pErrorProvider.SetError(cboCategory, "Please choose a category!");
                return;
            }

            if (txtName.TextBox.Text == "")
            {
                pErrorProvider.SetError(txtName.TextBox, "Please enter item name!");
                return;
            }

            if (txtDescription.Text == "")
            {
                pErrorProvider.SetError(txtDescription, "Please enter item description!");
                return;
            }


            if (validateInt(txtQuantity, "Please enter item quantity!") == false)
            {
                return;
            }



            if (validateInt(txtThreshold, "Please enter item Threshold!") == false)
            {
                return;
            }

            if (validateDecimal(txtPrice, "Please enter item Price!") == false)
            {
                return;
            }

            Controller.ItemController ic = new Controller.ItemController();

            try
            {
                bool ret = false;
                ret = ic.insertItem((int)cboCategory.SelectedValue, txtName.UserText, "","", txtDescription.Text, Convert.ToInt32(txtQuantity.Text), Convert.ToInt32(txtThreshold.Text), Convert.ToDecimal(txtPrice.Text), true );

                if (ret == true)
                {

                    SetMessage("Item Added successfully!");
                    reset();
                }
                else
                {
                    SetMessage("Error: Failed to add Item");

                }

            }
            catch (Exception ex)
            {
                CatchError(ex);
            }

        }



        private void btnReset_Click(object sender, EventArgs e)
        {
            reset();

        }

        private void reset()
        {

            this.txtName.TextBox.Text = "";
            this.txtDescription.Text = "";
            this.txtQuantity.Text = "";
            this.txtPrice.Text = "";
            this.txtThreshold.Text = "";

            pErrorProvider.Clear();

            this.txtName.Focus();

        }


        private void txtQuantity_TextChanged(object sender, EventArgs e)
        {

            validateInt(txtQuantity, "Please enter item quantity!");

        }

        private bool validateInt(Control ctrl, string strErrorMsg)
        {
            pErrorProvider.Clear();

            int ret;


            int.TryParse(ctrl.Text, out ret);

            if (ret < 1)
            {
                pErrorProvider.SetError(ctrl, strErrorMsg);
                ctrl.Focus();
                btnAdd.Enabled = false;
            }
            else { btnAdd.Enabled = true; }

            return (ret > 0);

        }


        private bool validateDecimal(Control ctrl, string strErrorMsg)
        {
            pErrorProvider.Clear();

            decimal ret;

            decimal.TryParse(ctrl.Text, out ret);

            if (ret < 1)
            {
                pErrorProvider.SetError(ctrl, strErrorMsg);
                ctrl.Focus();
                btnAdd.Enabled = false;
            }
            else { btnAdd.Enabled = true; }

            return (ret > 0);
        }



        private void txtThreshold_TextChanged(object sender, EventArgs e)
        {
            validateInt(txtThreshold, "Please enter item Threshold!");

        }

        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            validateDecimal(txtPrice, "Please enter item Price!");

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
